import AdManager from "../../Platforms/AdManager";
import LocalStorageData from "../../UIFrame/LocalStorageData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.ProgressBar)
    loading: cc.ProgressBar = null;

    @property(cc.Label)
    loadtxt: cc.Label = null;

    private loadTime = 0;
    private tpro: number = 0;
    private updateTpro: number = 0;
    private landId: number = 0;

    onLoad() {
        this.loadTime = 0;
        this.startLoad();
    }

    start() {
    }

    startLoad() {
        LocalStorageData.Instance().getUserData();

        this.tpro += 0.15;

        this.preloadScene('Main');
        this.loadResPro("prefabs/UIForms/");
    }

    loadResPro(url) {
        cc.resources.loadDir(url, cc.Prefab, this.loadProgress.bind(this), this.loadComplete.bind(this));
    }

    preloadScene(name) {
        cc.director.preloadScene(name, this.loadProgress.bind(this), this.loadComplete.bind(this));
    }

    loadProgress(completedCount: number, totalCount: number, item: any) {
        // console.log("资源加载中",completedCount,totalCount,item);
    }

    loadComplete(error: Error, resource: any[]) {
        cc.log(resource);
        this.tpro += 0.43;
    }

    update(dt) {
        if (this.updateTpro == 1) {
            return;
        }
        if (this.tpro >= this.updateTpro) {
            this.updateTpro += 0.01;
        }
        if (this.updateTpro > 1) {
            this.updateTpro = 1;
        }
        this.loading.progress = this.updateTpro;
        this.loadTime += dt;
        this.changeShowPro();
    }

    changeShowPro() {
        this.loadtxt.string = `加载中...${Math.floor(this.updateTpro * 100)} %`;
        if (this.updateTpro >= 1) {
            this.scheduleOnce(() => { cc.director.loadScene("Main"); }, 0.3);
        }
    }
}
